package tingshu.servlet.Home;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import tingshu.data_object.TrackDO;
import tingshu.repository.RelationRepo;
import tingshu.repository.TrackRepo;
import tingshu.util.DBUtil;
import tingshu.util.Log;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 专辑细节信息
 * @author houdaiqin
 * @date 2022/07/20 20:30
 **/
@WebServlet("/album-detail.json")
public class AlbumDetailServlet extends HttpServlet {
    public static class TrackView{
        public Integer tid;
        public String title;

        public TrackView(Integer tid, String title) {
            this.tid = tid;
            this.title = title;
        }
    }
    public static class ResultView{
        public Integer aid;
        public String username;
        public String title;
        public String cover;
        public List<TrackView> trackList=new ArrayList<>();
    }

    private final RelationRepo relationRepo=new RelationRepo();
    private final TrackRepo trackRepo=new TrackRepo();
    @Override
    @SneakyThrows
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int aid = Integer.parseInt(req.getParameter("aid"));
        ResultView resultView=new ResultView();
        try(Connection c= DBUtil.connection()){
            //联表查询，查找出专辑的相关信息 aid,username,title,cover
            String sql="select aid,username,title,cover from albums a join users u on a.uid = u.uid where aid=?";
            try(PreparedStatement ps=c.prepareStatement(sql)){
                ps.setInt(1,aid);
                Log.println("执行SQL"+sql);
                try(ResultSet rs=ps.executeQuery()){
                    if (!rs.next()) {
                        throw new RuntimeException("aid 对应的专辑不存在, 404");
                    }
                    resultView.aid=rs.getInt("aid");
                    resultView.username=rs.getString("username");
                    resultView.title=rs.getString("title");
                    resultView.cover=rs.getString("cover");
                }
            }
        }

        List<Integer> tidList=relationRepo.selectTidListByAid(aid);
        if (tidList.isEmpty()){
            throw new RuntimeException("这个专辑没有绑定任何音频");
        }else{
            //查找该专辑下音频的信息（tid，title）
            List<TrackDO> trackDOList=trackRepo.selectTrackListByTidList(tidList);
            for(TrackDO trackDO:trackDOList){
                TrackView trackView=new TrackView(trackDO.tid,trackDO.title);
                resultView.trackList.add(trackView);
            }
        }
        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(resultView);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        PrintWriter writer=resp.getWriter();
        writer.println(json);
    }
}